<#import "/layout/pageLayout.html" as pageLayout />

<#macro pageheading>
    <link href="/css/bootstrap/tables.css/" rel="stylesheet"/>
    <link href="/plugins/ALTE/plugins/iCheck/all.css" rel="stylesheet"/>
    <link rel="stylesheet" href="/plugins/bootstrap-datetimepicker/css/bootstrap-datetimepicker.min.css">
    <link rel="stylesheet" href="/plugins/daterangepicker/daterangepicker-bs3.css">
    <style type="text/css">
        td span {
            cursor: pointer
        }


    </style>
</#macro>
<#assign pageheading = pageheading in pageLayout />

<#macro pagecontent>

    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="box">
                    <div class="box-body">
                        <div class="dataTables" style="padding-top: 10px;">
                            <div class="form-inline " style="padding-bottom: 5px;">

                                <div class="form-group pull-right" >
                                    <div style="margin-right:100px;cursor: pointer; "
                                         onclick="addOrUpdate()"
                                         class="  margin  btn btn-info addBtn">+添加</div>
                                </div>

                            </div>
                            <div class="row">
                                <div class="col-md-12 cfgs_dataTable_div" style="overflow-x:auto; ">
                                    <table class=" table table-bordered   table-hover table-responsive" style="min-width: 1120px;">
                                        <thead>
                                        <tr>
                                            <th style="text-align: center;">ID</th>
                                            <th style="text-align: center;">名称</th>
                                            <th style="text-align: center;">服务器</th>
                                            <th style="text-align: center;">备份类型</th>
                                            <th style="text-align: center;">状态</th>
                                            <th style="text-align: center;">运行</th>
                                            <th style="text-align: center;">开始时间</th>
                                            <th style="text-align: center;">最后运行</th>
                                            <th style="text-align: center;">备份数据库</th>
                                            <th style="text-align: center;">自定义参数</th>
                                            <th style="text-align: center;">上传协议</th>
                                            <th style="text-align: center;">保留时长</th>
                                            <th style="text-align: center;">备注</th>
                                            <th style="text-align: center;">操作</th>

                                        </tr>
                                        </thead>
                                        <tbody>

                                        </tbody>
                                        <tfoot style="display: none;">

                                        <tr style="height: 30px;">
                                            <td><span class="field" field-name="p_id"></span></td>
                                            <td><span class="field" field-name="p_name"></span></td>
                                            <td><span class="field" field-name="p_host"></span>:<span class="field" field-name="p_port"></span></td>                                            
                                            <td><span class="field" field-name="p_backType"></span></td>
                                            <td><span class="field" field-name="p_state"></span></td>
                                            <td><span class="field" field-name="p_runsSate"></span></td>
                                            <td><span class="field" field-name="p_beginTime"></span></td>
                                            <td><span class="field" field-name="p_lastTime"  ></span></td>
                                            <td><span class="field" field-name="p_dbAndTables"></span></td>
                                            <td><span class="field" field-name="p_backupParmas"></span></td>
                                            <td><span class="field" field-name="p_remoteType"></span></td>
                                            <td><span class="field" field-name="p_retentionTime"  ></span></td>
                                            <td><span class="field" field-name="p_remarks"></span></td>
                                            <td>
                                                <span class="text-green run_btn"> 运行 </span> |
                                                <span class="text-green rsync_btn"> rsync </span> |
                                                <span class="text-green clone_btn"> 克隆 </span> |
                                                <span class="text-green edit_btn"> 修改 </span> |
                                                <span class="text-green delete_btn"> 删除 </span>
                                            </td>
                                        </tr>
                                        </tfoot>
                                    </table>
                                </div>
                                <div class="col-sm-12 ">
                                    <div class="pagination pull-right" id="device-data-pagination"></div>
                                </div>
                            </div>

                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-12 dialog_template" style="display: none;">

                <div class="edit_div">
                    <div style="overflow-y: auto;overflow-x: hidden;">
                        <div class="form-horizontal">
                            <div class="box-body" >
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">
                                        名称
                                    </label>
                                    <div class="col-sm-10">
                                        <input type="text" data-name="p_name"  class="form-control  feild-edit" value=""/>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">
                                        状态
                                    </label>
                                    <div class="col-sm-10">
                                        <select data-name="p_state" class="form-control  feild-edit">
                                            <option value="0" >停用</option>
                                            <option value="1" selected>正常</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">
                                        服务器地址
                                    </label>
                                    <div class="col-sm-10">
                                        <input type="text" data-name="p_host"  class="form-control  feild-edit" value="localhost"/>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">
                                        端口
                                    </label>
                                    <div class="col-sm-10">
                                        <input type="number" data-name="p_port"  class="form-control  feild-edit" value="3306"/>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">
                                        用户名
                                    </label>
                                    <div class="col-sm-10">
                                        <input type="text" data-name="p_user"  class="form-control  feild-edit" value="root"/>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">
                                        密码
                                    </label>
                                    <div class="col-sm-10">
                                        <input type="password" data-name="password"  class="form-control  feild-edit" value=""/>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">
                                        备份类型
                                    </label>
                                    <div class="col-sm-10">
                                        <select data-name="p_backType" class="form-control  feild-edit">
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">
                                        压缩备份
                                    </label>
                                    <div class="col-sm-10">
                                        <select data-name="p_compress" class="form-control  feild-edit">
                                            <option value="false" >不压缩</option>
                                            <option value="true" selected>压缩</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">
                                        自定义参数
                                    </label>
                                    <div class="col-sm-10">
                                        <input type="text" data-name="p_backupParmas"  class="form-control  feild-edit" value=""/>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">
                                        保留时长(天)
                                    </label>
                                    <div class="col-sm-10">
                                        <input type="number" data-name="p_retentionTime"  class="form-control  feild-edit" value="14"/>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">
                                        备注
                                    </label>
                                    <div class="col-sm-10">
                                        <input type="text" data-name="p_remarks"  class="form-control  feild-edit" value=""/>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">
                                        备份方式
                                    </label>
                                    <div class="col-sm-10">
                                        <select class="form-control  feild-edit"  data-name="p_backupMode"></select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">
                                        执行计划
                                    </label>
                                    <div class="col-sm-10"> 
                                        <input class="form-control  feild-edit"  data-name="p_corn" value="" placeholder="请输入cron表达式" />                      
                                    </div>
                                </div>
                                <div class="form-group"><span class="text-red add_error_note"></span></div>
                                
                                <div class="form-group">
                                    <label class="col-sm-12 text-red">
                                        数据库配置
                                    </label>
                                    <table class=" table table-bordered   table-hover table-responsive dataBaseTable" style="min-width: 1120px;">
                                        <thead>
                                        <tr>
                                            <th style="text-align: center;">数据库</th>
                                            <th style="text-align: center;">指定表(不设置则备份整个库)</th>
                                            <th style="text-align: center;">操作</th>

                                        </tr>
                                        </thead>
                                        <tbody>
                                        </tbody>
                                        <tfoot style="display: none;">
                                            <tr style="height: 30px;">
                                                <td><span class="field" field-name="dbname"></span></td>
                                                <td><span class="field" field-name="tables"  ></span></td>
                                                <td>
                                                    <span class="text-green update_btn"> 修改 </span> |
                                                    <span class="text-green delete_btn"> 删除 </span>
                                                </td>
                                            </tr>
                                        </tfoot>
                                    </table>
                                </div>
                                <div class="form-group"  style="display: none;">
                                    <label class="col-sm-12 text-red">
                                        执行计划
                                    </label>
                                    <table class=" table table-bordered   table-hover table-responsive BackupCornTable" style="min-width: 1120px;">
                                        <thead>
                                        <tr>
                                            <th style="text-align: center;">备份方式</th>
                                            <th style="text-align: center;">周期类型</th>
                                            <th style="text-align: center;">周期序列</th>
                                            <th style="text-align: center;">操作</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        </tbody>
                                        <tfoot style="display: none;">
                                            <tr style="height: 30px;">
                                                <td><span class="field" field-name="p_backupMode"></span></td>
                                                <td><span class="field" field-name="p_timeType"  ></span></td>
                                                <td><span class="field" field-name="p_timeIndex"  ></span></td>
                                                <td>
                                                    <span class="text-green update_btn"> 修改 </span> |
                                                    <span class="text-green delete_btn"> 删除 </span>
                                                </td>
                                            </tr>
                                        </tfoot>
                                    </table>
                                </div>
                                <div class="form-group" >
                                    <label class="col-sm-12 text-red">
                                        备份文件远程存储
                                    </label>
                                    <table class=" table table-bordered   table-hover table-responsive remoteStoreTable" style="min-width: 1120px;">
                                        <thead>
                                        <tr>
                                            <th style="text-align: center;"></th>
                                            <th style="text-align: center;">类型</th>
                                            <th style="text-align: center;">配置</th>
                                            <th style="text-align: center;">操作</th>

                                        </tr>
                                        </thead>
                                        <tbody>
                                        </tbody>
                                        <tfoot style="display: none;">
                                            <tr style="height: 30px;">
                                                <td><input type="checkbox" class="field"  field-name="remoteTypeCheck" /></td>
                                                <td><span class="field" field-name="remoteType"></span></td>
                                                <td><span class="field" field-name="remoteCfg"  ></span></td>
                                                <td>
                                                    <span class="text-green update_btn"> 修改 </span> |
                                                    <span class="text-green delete_btn"> 删除 </span>
                                                </td>
                                            </tr>
                                        </tfoot>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
                <div class="dataBaseEdit">                    
                    <div style="overflow-y: auto;overflow-x: hidden;">
                        <div class="form-horizontal">
                            <div class="box-body" >
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">
                                        数据库
                                    </label>
                                    <div class="col-sm-10">
                                        <input type="text" data-name="dbname"  class="form-control  feild-edit" value=""/>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">
                                        指定表
                                    </label>
                                    <div class="col-sm-10">
                                        <input type="text" data-name="tables"  class="form-control  feild-edit" value="" placeholder="多个之间用逗号分隔,(不设置则备份整个库)"/>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                </div>
                <div class="remoteStoreEdit">
                    <div style="overflow-y: auto;overflow-x: hidden;">
                        <div class="form-horizontal">
                            <div class="box-body" >
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">
                                        远程存储类型
                                    </label>
                                    <div class="col-sm-10">
                                        <select  data-name="remoteType" class="form-control  feild-edit">
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group"  data-type="cfg" allow-types=",6," style="display: none;">
                                    <label class="col-sm-2 control-label">
                                        远程地址
                                    </label>
                                    <div class="col-sm-10">
                                        <input   type="text" data-name="p_host"  class="form-control  feild-edit" value="" placeholder="" />
                                    </div>
                                </div>
                                <div class="form-group"  data-type="cfg" allow-types=",6,"  style="display: none;">
                                    <label class="col-sm-2 control-label">
                                        远程端口
                                    </label>
                                    <div class="col-sm-10">
                                        <input type="number" data-name="p_port"  class="form-control  feild-edit" value="" placeholder=""/>
                                    </div>
                                </div>
                                <div class="form-group"   data-type="cfg" allow-types=",6,"  style="display: none;">
                                    <label class="col-sm-2 control-label">
                                        用户名
                                    </label>
                                    <div class="col-sm-10">
                                        <input  type="text" data-name="p_user"  class="form-control  feild-edit" value="" placeholder=""/>
                                    </div>
                                </div>
                                <div class="form-group"   data-type="cfg" allow-types=",6,"  style="display: none;">
                                    <label class="col-sm-2 control-label">
                                        密码文件
                                    </label>
                                    <div class="col-sm-10">
                                        <input  data-type="cfg" allow-types=",6," type="text" data-name="p_pwd"  class="form-control  feild-edit" value="" placeholder=""/>
                                    </div>
                                </div>
                                <div class="form-group"   data-type="cfg" allow-types=",6,"  style="display: none;">
                                    <label class="col-sm-2 control-label">
                                        远程根目录
                                    </label>
                                    <div class="col-sm-10">
                                        <input  data-type="cfg" allow-types=",6," type="text" data-name="p_baseDir"  class="form-control  feild-edit" value="" placeholder=""/>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                   
                </div>
                <div class="BackupCornEdit">
                    <div style="overflow-y: auto;overflow-x: hidden;">
                        <div class="form-horizontal">
                            <div class="box-body" >
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">
                                        备份方式
                                    </label>
                                    <div class="col-sm-10">
                                        <select class="form-control  feild-edit"  data-name="p_backupMode"></select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">
                                        周期类型
                                    </label>
                                    <div class="col-sm-10">                           
                                        <select class="form-control  feild-edit"  data-name="p_timeType"></select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">
                                        周期序列
                                    </label>
                                    <div class="col-sm-10">                           
                                        <div class="select_index" >
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                </div>
            </div>

        </div>
    </section>

</#macro>
<#assign pagecontent = pagecontent in pageLayout />

<#macro pagefooter>

    <script src="/plugins/ALTE/plugins/iCheck/icheck.min.js" type="text/javascript"></script>
    <script src="/plugins/ALTE/plugins/datatables/jquery.dataTables.min.js" type="text/javascript"></script>
    <script src="/plugins/moment/moment.js"></script>
    <script src="/plugins/moment/locale/zh-cn.js"></script>
    <script src="/plugins/bootstrap-datetimepicker/js/bootstrap-datetimepicker.min.js?v=1"></script>
    <script src="/plugins/daterangepicker/daterangepicker.js"></script>

    <script type="text/javascript" src="/plugins/bootstrap-select/bootstrap-select.js"></script>
    <link rel="stylesheet" type="text/css" href="/plugins/bootstrap-select/bootstrap-select.css" />
    <script src="/plugins/cronGen/cronGen.js"></script>

    <script type="text/javascript">
        /**
         * Created by Eric.Zhang on 2017/1/7.
         */
        var backupSttingPage_operat = {
            solt_table: '',
            data:{}

        }
        backupSttingPage_operat.data.runstate={
            valueNames:[{value:0,name:"空闲"},{value:1,name:"运行中"}],
            getName:function (value) {
                var result = '';
                $(this.valueNames).each(function (i, vn) {
                    if (vn.value == value) {
                        result = vn.name;
                        return false;
                    }
                });
                return result;
            }
        }
        backupSttingPage_operat.data.state={
            valueNames:[{value:1,name:"正常"},{value:0,name:"停用"}],
            getName:function (value) {
                var result = '';
                $(this.valueNames).each(function (i, vn) {
                    if (vn.value == value) {
                        result = vn.name;
                        return false;
                    }
                });
                return result;
            }
        }
        backupSttingPage_operat.data.timeType={
            valueNames:[{value:1,name:"月"},{value:2,name:"周"},{value:3,name:"日"}],
            getName:function (value) {
                var result = '';
                $(this.valueNames).each(function (i, vn) {
                    if (vn.value == value) {
                        result = vn.name;
                        return false;
                    }
                });
                return result;
            }
        }
        backupSttingPage_operat.data.backupMode={
            valueNames:[{value:1,name:"全量备份"},{value:2,name:"增量备份"}],
            getName:function (value) {
                var result = '';
                $(this.valueNames).each(function (i, vn) {
                    if (vn.value == value) {
                        result = vn.name;
                        return false;
                    }
                });
                return result;
            }
        }
        backupSttingPage_operat.data.backType={
            valueNames:[{value:0,name:"mysqldump"},{value:1,name:"innobackupex24"},{value:2,name:"mariabackup"}],
            getName:function (value) {
                var result = '';
                $(this.valueNames).each(function (i, vn) {
                    if (vn.value == value) {
                        result = vn.name;
                        return false;
                    }
                });
                return result;
            }
        }
        backupSttingPage_operat.data.remoteType={
            valueNames:[{value:1,name:"ftp"},{value:2,name:"sftp"},{value:3,name:"http"},{value:4,name:"百度云盘"},{value:5,name:"阿里云oss"},{value:6,name:"rsync"}],
            getName:function (value) {
                var result = '';
                $(this.valueNames).each(function (i, vn) {
                    if (vn.value == value) {
                        result = vn.name;
                        return false;
                    }
                });
                return result;
            }
        }
        function getList(){
            $('.cfgs_dataTable_div tbody').empty();
            $.get('/CfgApi/getBackupTaskModels',function (data) {
                if(data){
                    $('.cfgs_dataTable_div tbody').empty();
                    $(data).each(function (o,obj) {
                        if(obj){
                            showModel(obj);
                        }
                    })
                }
            })
        }
        function showModel(model,tr){
            if(!tr){
                tr = $($('.cfgs_dataTable_div tfoot').html());
                $('.cfgs_dataTable_div tbody').append(tr);
            }
            $(tr).find(".field").each(function (j, span) {
                var feildName = $(span).attr("field-name");
                if(feildName == "p_beginTime"||feildName == "p_lastTime"){
                    if(model[feildName]){
                        $(span).html(new Date(model[feildName]).format("yyyy-MM-dd hh:mm:ss"));
                    }
                }else  if (feildName == "p_state") {
                    $(span).html(backupSttingPage_operat.data.state.getName(model[feildName]));
                } else if(feildName == "p_runsSate"){
                    $(span).html(backupSttingPage_operat.data.runstate.getName(model[feildName]));
                }else {
                    $(span).html(model[feildName]);
                }

            });

            $(tr).find(".run_btn").unbind("click").click(function () {
                $.ajax({
                    url: "/CfgApi/runBackupTaskModel",
                    data: {id:model.p_id},
                    type: 'post',
                    cache: false,
                    success: function (txt) {
                        dialog({
                            title: '成功',
                            content:  '运行成功',
                            quickClose: true,
                        }).show();
                        clearInterval(_interval);
                    }
                });
                getList();
               var _interval = setInterval(getList,1000*30) ;
            });
            $(tr).find(".clone_btn").unbind("click").click(function () {
                var _model = JSON.parse(JSON.stringify(model));
                _model.p_id='';
                _model.p_name='';
                _model.p_dbAndTables=[];
                addOrUpdate(_model,null);
            });
            $(tr).find(".rsync_btn").unbind("click").click(function () {
                $.ajax({
                    url: "/CfgApi/runRsync",
                    data: {id:model.p_id},
                    type: 'post',
                    cache: false,
                    success: function (txt) {
                        dialog({
                            title: '成功',
                            content:  '运行成功',
                            quickClose: true,
                        }).show();
                        clearInterval(_interval);
                    }
                });
            });
            $(tr).find(".edit_btn").unbind("click").click(function () {
                addOrUpdate(model,tr);
            });
            $(tr).find(".delete_btn").unbind("click").click(function () {
                var d = dialog({
                    title: '删除' ,
                    content: "确定删除"+model.p_name+"吗",
                    okValue: "删除",
                    ok: function () {
                        var url =  "/CfgApi/removeBackupTaskModel" ;
                        $.ajax({
                            url: url,
                            data: {id:model.p_id},
                            type: 'post',
                            cache: false,
                            success: function (txt) {
                                d.close().remove();
                                dialog({
                                    title: '成功',
                                    content:  '删除成功',
                                    quickClose: true,
                                }).show();
                                $(tr).remove();
                            }
                        });
                        return false;
                    },
                    cancel: true,
                    cancelValue: "取消"
                }).showModal();
            });
        }
        function addOrUpdate(model,tr){
            var isAdd = model==null;
            var diaElement = $($(".edit_div").html()).height($(window).height()-200);
            var title = (isAdd ? "添加" : "修改");
            if (isAdd) {
                model = {p_runsSate: '0'};
            }else{
                $(diaElement).find(".feild-edit").each(function (i, ele) {
                    var dataName=$(ele).attr("data-name")
                    if(dataName=='p_compress'){
                        $(ele).val(model[dataName]+'');
                    }else{
                        $(ele).val(model[dataName]);
                    }
                });
                if(model.p_dbAndTables){
                    $.each(model.p_dbAndTables,function(db,value){
                        showDataBaseModel(null,{"dbName":db,tables:value});
                    })
                    
                }
                if(model.p_remoteType){
                    $.each(model.p_remoteType,function(i,remoteType){
                        showRemoteStoreModel(null,{"remoteType":remoteType,"remoteCfg":model.p_remoteCfg[remoteType]})
                    })
             
                }
                // if(model.p_backupCorns){
                //     $.each(model.p_backupCorns,function(i,cornObj){
                //         showBackupCornModel(null,cornObj);
                //     })
             
                // }
            }
            // $(diaElement).find('.feild-edit[data-name="p_corn"]').cronGen({
            //     direction : 'right'
            //     }); 
            var width = $(window).width();
            width = width - 250;
            if (width < 100) {
                width = 100;
            }
       
            var d = dialog({
                title: title ,
                content: diaElement,
                width: width+'px',
                button:[
                    {
                        value: '添加数据库',
                        callback: function () {
                            editDataBaseModel(null,null)
                            return false;
                        }
                    },
                    {
                        value: '添加远程存储',
                        callback: function () {
                            editRemoteStoreModel(null,null)
                            return false;
                        }
                    }
                ],
                okValue: title,
                ok: function () {
                    var postObj=JSON.parse(JSON.stringify(model));
                    $(diaElement).find(".feild-edit").each(function (i, ele) {
                        postObj[$(ele).attr("data-name")] = $(ele).val();
                    })
                    postObj.p_dbAndTables={};
                    postObj.p_remoteType=[];
                    postObj.p_remoteCfg={};
                    $(diaElement).find('.dataBaseTable tbody tr').each(function(i,tr){
                        var dbName = $(tr).find('.field[field-name="dbname"]').text();
                        if(dbName){
                            postObj.p_dbAndTables[dbName]=[];
                            var tables=$(tr).find('.field[field-name="tables"]').text();
                            if(tables){
                                $(tables.split(',')).each(function(j,table){
                                    if(table){                                        
                                        postObj.p_dbAndTables[dbName].push(table);
                                    }
                                })
                            }
                        }
                    })
                    // postObj.p_backupCorns=[];
                    // $(diaElement).find('.BackupCornTable tbody tr').each(function(i,tr){

                    //     var p_backupMode = $(tr).find('.field[field-name="p_backupMode"]').attr("value");
                    //     var p_timeType = $(tr).find('.field[field-name="p_timeType"]').attr("value");
                    //     var p_timeIndex=[];
                    //     $(tr).find('.field[field-name="p_timeIndex"] .timeIndex').each(function(j,span){
                    //         p_timeIndex.push($(span).attr("value"))
                    //     })
                    //     postObj.p_backupCorns.push({
                    //         p_backupMode:p_backupMode,
                    //         p_timeType:p_timeType,
                    //         p_timeIndex:p_timeIndex
                    //     })
                    // })
                    $(diaElement).find('.remoteStoreTable tbody tr').each(function(i,tr){
                        var remoteType = $(tr).find('.field[field-name="remoteType"]').attr("value");
                        if(remoteType){
                            postObj.p_remoteType.push(remoteType);
                            postObj.p_remoteCfg[remoteType]=$(tr).find('.field[field-name="remoteCfg"]').text();
                            
                        }
                    })
                    if (postObj.p_name.trim()=="") {
                        dialog({
                            content: '请填写名称',
                            quickClose: true,
                        }).show();
                        return false;
                    }else{
                        postObj.p_name=encodeURI(postObj.p_name);
                    }
                    if (postObj.p_host.trim()=="") {
                        dialog({
                            content: '请填写数据库连接',
                            quickClose: true,
                        }).show();
                        return false;
                    }
                    if (postObj.p_port.trim()=="") {
                        dialog({
                            content: '请填写数据库端口',
                            quickClose: true,
                        }).show();
                        return false;
                    }
                    if (postObj.p_user.trim()=="") {
                        dialog({
                            content: '请填写数据库连接用户',
                            quickClose: true,
                        }).show();
                        return false;
                    }
                    if (postObj.p_corn.trim()=="") {
                        dialog({
                            content: '请填写执行计划',
                            quickClose: true,
                        }).show();
                        return false;
                    }
                    if (postObj.p_retentionTime.trim()=="") {
                        dialog({
                            content: '请填写备份文件保留时间',
                            quickClose: true,
                        }).show();
                        return false;
                    }


                    var url =  "/CfgApi/setBackupTaskModel" ;
                    $.ajax({
                        url: url,
                        data: JSON.stringify( postObj ),
                        contentType:"application/json",
                        type: 'post',
                        cache: false,
                        success: function (txt) {
                            if (txt) {
                                d.close().remove();
                                dialog({
                                    title: '成功',
                                    content: title + '成功',
                                    quickClose: true,
                                }).show();

                                showModel(txt,tr);
                            } else {
                                dialog({
                                    title: '失败',
                                    content: '添加失败',
                                    quickClose: true,
                                }).show();
                                return false;
                            }

                        }
                    });
                    return false;
                },
                cancel: true,
                cancelValue: "取消"
            }).showModal();

            function showDataBaseModel(tr,obj){
                if(!tr){
                    tr=$($(diaElement).find('.dataBaseTable tfoot').html())
                    $(diaElement).find('.dataBaseTable tbody').append(tr);
                }
                tr.find('.field[field-name="dbname"]').html(obj.dbName);
                tr.find('.field[field-name="tables"]').html(obj.tables.join(','));
                $(tr).find('.delete_btn').click(function(){
                    $(tr).remove();
                })
                $(tr).find('.update_btn').click(function(){
                    editDataBaseModel(tr,obj);
                })
            }
            function editDataBaseModel(tr,obj){
                var isAdd = obj?false:true;
                var diaDiv = $($('.dataBaseEdit').html());
                var dd = dialog({
                    title: isAdd?"添加":"修改" ,
                    content: diaDiv,
                    width: '40em',
                    okValue: isAdd?"添加":"修改",
                    ok: function () {
                        showDataBaseModel(tr,{"dbName":diaDiv.find('.feild-edit[data-name="dbname"]').val(),"tables":diaDiv.find('.feild-edit[data-name="tables"]').val().split(',')})
                    },
                    cancel: true,
                    cancelValue: "取消"
                });
                dd.showModal();
                if(obj){
                    diaDiv.find('.feild-edit[data-name="dbname"]').val(obj.dbName);
                    diaDiv.find('.feild-edit[data-name="tables"]').val(obj.tables.join(','));
                }

            }
            function showBackupCornModel(tr,obj){
                if(!tr){
                    tr=$($(diaElement).find('.BackupCornTable tfoot').html())
                    $(diaElement).find('.BackupCornTable tbody').append(tr);
                }
                $(tr).find('.field').each(function(i,span){
                    var fieldName=$(span).attr('field-name');
                    if(fieldName=='p_backupMode'){
                        $(span).html(backupSttingPage_operat.data.backupMode.getName(obj[fieldName])).attr("value",obj[fieldName]);
                    }
                    if(fieldName=='p_timeType'){
                        $(span).html(backupSttingPage_operat.data.timeType.getName(obj[fieldName])).attr("value",obj[fieldName]);
                    }
                    if(fieldName=='p_timeIndex'){
                        if(obj[fieldName]){
                            $(obj[fieldName]).each(function(j,timeIndex){
                                if(obj["p_timeType"]==1){
                                    $(span).append('<span class="timeIndex margin" value="'+timeIndex+'">'+timeIndex+'日</span>');
                                }else  if(obj["p_timeType"]==2){
                                    $(span).append('<span class="timeIndex margin" value="'+timeIndex+'">周'+(timeIndex==7?'日':timeIndex)+'</span>');
                                }else  if(obj["p_timeType"]==3){
                                    $(span).append('<span class="timeIndex margin" value="'+timeIndex+'">'+timeIndex+'时</span>');
                                }
                            })
                            
                        }
                        
                    }
                })
                $(tr).find('.delete_btn').click(function(){
                    $(tr).remove();
                })
                $(tr).find('.update_btn').click(function(){
                    BackupCornEdit(tr,obj);
                })
            }
            function editBackupCornModel(tr,obj){
                var diaDiv = $($('.BackupCornEdit').html());
                $(backupSttingPage_operat.data.timeType.valueNames).each(function(i,timeType){
                    diaDiv.find('.feild-edit[data-name="p_timeType"]').append('<option value="'+timeType.value+'" '+(i==0?'selected':'')+'>'+timeType.name+'</option>')
                    var div = $('<div class="select_index_checks" timeType="'+timeType.value+'" ></div>')
                    diaDiv.find('.select_index').append(div);
                    if(timeType.value==1){                        
                        for(var a=1;a<32;a++){
                            div.append('<label class="margin"><input type="checkbox" value="'+a+'">'+a+'日</label>')
                        }
                    }else if(timeType.value==2){                        
                        for(var a=1;a<8;a++){
                            div.append('<label class="margin"><input type="checkbox" value="'+a+'">周'+(a==7?"日":a)+'</label>')
                        }
                    }else if(timeType.value==3){                        
                        for(var a=0;a<24;a++){
                            div.append('<label class="margin"><input type="checkbox" value="'+a+'">'+a+'时</label>')
                        }
                    }
                })
                $(backupSttingPage_operat.data.backupMode.valueNames).each(function(i,backupMode){
                    diaDiv.find('.feild-edit[data-name="p_backupMode"]').append('<option value="'+backupMode.value+'" '+(i==0?'selected':'')+'>'+backupMode.name+'</option>')
                   
                })
                diaDiv.find('.feild-edit[data-name="p_timeType"]').change(function(){
                    timeTypechange(diaDiv.find('.feild-edit[data-name="p_timeType"]').val());
                })
                if(obj){
                    diaDiv.find('.feild-edit[data-name="p_timeType"]').val( obj.p_timeType);
                    diaDiv.find('.feild-edit[data-name="p_backupMode"]').val(obj.p_backupMode);
                }
                timeTypechange(diaDiv.find('.feild-edit[data-name="p_timeType"]').val())
                var dd = dialog({
                    title: isAdd?"添加":"修改" ,
                    content: diaDiv,
                    width: '40em',
                    okValue: isAdd?"添加":"修改",
                    ok: function () {
                        if(!obj){
                            obj={}
                        }
                        obj.p_timeType = diaDiv.find('.feild-edit[data-name="p_timeType"]').val();
                        obj.p_backupMode = diaDiv.find('.feild-edit[data-name="p_backupMode"]').val();
                        obj.p_timeIndex=[];
                        diaDiv.find('.select_index .select_index_checks[timeType="'+obj.p_timeType+'"] input:checked').each(function(j,input){
                            obj.p_timeIndex.push($(input).val());
                        });
                        showBackupCornModel(tr,obj)
                    },
                    cancel: true,
                    cancelValue: "取消"
                });
                dd.showModal();
                function timeTypechange(value){
                    diaDiv.find('.select_index .select_index_checks[timeType="'+value+'"]').show().siblings().hide();
                }
            }
            function showRemoteStoreModel(tr,obj){
                if(!tr){
                    tr=$($(diaElement).find('.remoteStoreTable tfoot').html())
                    $(diaElement).find('.remoteStoreTable tbody').append(tr);
                }
                $(tr).find('.field').each(function(i,span){
                    var fieldName=$(span).attr('field-name');
                    if(fieldName=='remoteType'){
                        $(span).html(backupSttingPage_operat.data.remoteType.getName(obj[fieldName])).attr("value",obj[fieldName]);
                    }
                    if(fieldName=='remoteCfg'){
                        $(span).html(obj[fieldName]);
                    }
                   
                })
                $(tr).find('.delete_btn').click(function(){
                    $(tr).remove();
                })
                $(tr).find('.update_btn').click(function(){
                    editRemoteStoreModel(tr,obj);
                })
            }
            function editRemoteStoreModel(tr,obj){
                var diaDiv = $($('.remoteStoreEdit').html());
                $(backupSttingPage_operat.data.remoteType.valueNames).each(function(i,timeType){
                    diaDiv.find('.feild-edit[data-name="remoteType"]').append('<option value="'+timeType.value+'" '+(i==0?'selected':'')+'>'+timeType.name+'</option>')
                })
                diaDiv.find('.feild-edit[data-name="remoteType"]').change(showRemoteTypeForm)
               
                function showRemoteTypeForm(){
                    var remoteType = diaDiv.find('.feild-edit[data-name="remoteType"]').val();
                    diaDiv.find('.form-group[data-type="cfg"]').each(function(i,formGroup){
                        var allowTypes = $(formGroup).attr("allow-types");
                        if(allowTypes.indexOf(','+remoteType+",")>-1){
                            $(formGroup).show()
                        }
                    });
                    dd.reset();

                }
                 var dd = dialog({
                    title: isAdd?"添加":"修改" ,
                    content: diaDiv,
                    width: '40em',
                    okValue: isAdd?"添加":"修改",
                    ok: function () {
                        var cfgObj = {}
                        diaDiv.find('.form-group[data-type="cfg"]').each(function(i,formGroup){
                                    if(!$(formGroup).is(":hidden")){
                                        var feildInput = $(formGroup).find(".feild-edit");
                                        cfgObj[feildInput.attr('data-name')]=feildInput.val()
                                    }
                                });
                        if(!obj){
                            obj={}
                        }
                        obj.remoteType = diaDiv.find('.feild-edit[data-name="remoteType"]').val();
                        obj.remoteCfg = JSON.stringify(cfgObj);
                        
                        showRemoteStoreModel(tr,obj)
                    },
                    cancel: true,
                    cancelValue: "取消"
                });
                dd.showModal();
                if(obj){
                    diaDiv.find('.feild-edit[data-name="remoteType"]').val(obj.remoteType);
                    showRemoteTypeForm();
                    var cfgObj = {}
                    if(obj.remoteCfg){
                        cfgObj = JSON.parse(obj.remoteCfg);
                        if(cfgObj){
                            diaDiv.find('.form-group[data-type="cfg"]').each(function(i,formGroup){
                                if(!$(formGroup).is(":hidden")){
                                    var feildInput = $(formGroup).find(".feild-edit");
                                    feildInput.val(cfgObj[feildInput.attr('data-name')])
                                }
                            });
                        }
                    }
                    dd.reset();
                }
               
            }
           
        }
        $(function () {
            $.each(backupSttingPage_operat.data.backType.valueNames,function(i,vm){
                $('.edit_div .feild-edit[data-name="p_backType"]').append('<option value="'+vm.value+'" '+(i==0?'selected':'')+'>'+vm.name+'</option>')
            })
            $.each(backupSttingPage_operat.data.backupMode.valueNames,function(i,vm){
                $('.edit_div .feild-edit[data-name="p_backupMode"]').append('<option value="'+vm.value+'" '+(i==0?'selected':'')+'>'+vm.name+'</option>')
            })
            
            getList();
        });
    </script>
</#macro>
<#assign pagefooter = pagefooter in pageLayout />
<@pageLayout.pagetitle title="数据库列表"/>